﻿using AutoMapper;
using B.S.Traceability.Domain;
using B.S.Traceability.ErrorCode;
using B.S.Traceability.Infra.Interfaces;
using B.S.Traceability.WebAPI.Write.Commands;
using MediatR;

namespace B.S.Traceability.WebAPI.Write.CommandHandlers
{
    public class ShopsCommandHandler : IRequestHandler<ShopsCommand, APIResult<object>>
    {
        private readonly IBaseRepository<Shops> _shopsRepository;
        private readonly IMapper _mapper;

        public ShopsCommandHandler(IBaseRepository<Shops> shopsRepository, IMapper mapper)
        {
            _shopsRepository = shopsRepository;
            _mapper = mapper;
        }

        public async Task<APIResult<object>> Handle(ShopsCommand request, CancellationToken cancellationToken)
        {
            APIResult<object> result = new APIResult<object>();
            result.Code = APIEnum.Success;
            result.Msg = "添加成功";

            var entity = _mapper.Map<Shops>(request);
            entity.CreatePerson = "张三";
            entity.CreateTime = DateTime.Now;
            entity.isDel = false;
            _shopsRepository.Add(entity);

            return await Task.FromResult(result);
        }
    }
}
